package com.bjsxt.controller.system;

import com.bjsxt.aspectj.annotation.Log;
import com.bjsxt.aspectj.enums.BusinessType;
import com.bjsxt.domain.Role;
import com.bjsxt.dto.RoleDto;
import com.bjsxt.service.RoleService;
import com.bjsxt.utils.ShiroSecurityUtils;
import com.bjsxt.vo.AjaxResult;
import com.bjsxt.vo.DataGridView;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:liao
 * @Description:TODO 角色
 * @date:2020/12/11
 */
@RestController
@RequestMapping("/system/role/")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 分页查询
     * @param roleDto
     * @return
     */
    @GetMapping("listRoleForPage")
    public AjaxResult listRoleForPage(RoleDto roleDto){
        DataGridView dataGridView = roleService.listRolePage(roleDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }

    /**
     * 查询有效角色
     * @return
     */
    @GetMapping("selectAllRoles")
    public AjaxResult selectAllRoles(){
        List<Role> roleList = roleService.listAllRoles();
        return AjaxResult.success(roleList);
    }

    /**
     * 根据角色id查询角色信息
     * @param roleId
     * @return
     */
    @GetMapping("getRoleById/{roleId}")
    public AjaxResult getRoleById(@PathVariable Long roleId){
        Role role = roleService.getOne(roleId);
        return AjaxResult.success(role);
    }

    /**
     * 添加角色
     * @param roleDto
     * @return
     */
    @PostMapping("addRole")
    @Log(title = "添加角色",businessType = BusinessType.INSERT)
    public AjaxResult addRole(@Validated RoleDto roleDto){
        roleDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        int i = roleService.addRole(roleDto);
        return AjaxResult.toAjax(i);
    }

    @PutMapping("updateRole")
    @Log(title = "修改角色",businessType = BusinessType.UPDATE)
    public AjaxResult updateRole(@Validated RoleDto roleDto){
        roleDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(roleService.updateRole(roleDto));
    }

    /**
     * 根据角色id删除角色
     * @param roleIds
     * @return
     */
    @DeleteMapping("deleteRoleByIds/{roleIds}")
    @Log(title = "删除角色",businessType = BusinessType.DELETE)
    public AjaxResult deleteRoleByIds(@PathVariable Long[] roleIds){
        return AjaxResult.toAjax(roleService.deleteRoleByIds(roleIds));
    }

    /**
     * 保存角色和菜单之间的关系
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("saveRoleMenu/{roleId}/{menuIds}")
    public AjaxResult saveRoleMenu(@PathVariable Long roleId,@PathVariable Long[] menuIds){
        if (menuIds.length==1&&menuIds[0].equals(-1L)){
            menuIds = new Long[]{};
        }
        roleService.saveRoleMenu(roleId,menuIds);
        return AjaxResult.success();
    }

    /**
     * 根据用户id查询用户角色id列表
     * @param userId
     * @return
     */
    @GetMapping("getRoleIdsByUserId/{userId}")
    public AjaxResult getRoleIdsByUserId(@PathVariable Long userId){
        return AjaxResult.success(roleService.getRoleIdsByUserId(userId));
    }


    /**
     * 保存用户和角色之间的关系
     */
    @PostMapping("saveRoleUser/{userId}/{roleIds}")
    public AjaxResult saveRoleUser(@PathVariable Long userId,@PathVariable Long[] roleIds){
        if (roleIds.length==1&&roleIds[0].equals(-1L)){
            roleIds = new Long[]{};
        }
        roleService.saveRoleUser(userId,roleIds);
        return AjaxResult.success();
    }

}
